/**
 * Member controller js code
 */
define(function (require,exports,module){
	require("ajaxproxy");
	require("jdialog");
	var common = require('common');
	var template = require("jtemplate");
	template.config("openTag", "{[");
	template.config("closeTag", "]}");

	exports.init = function(configs){

		//初始化swich插件
		common.bswitctListInit(configs);
		common.bswitchAddInit();

		//添加数据
		$("#content-add").on("click", function() {
			exports.post({status:'1', index_show: 1}, configs.insert_url);
		});

		//编辑数据
		$(".item-edit").on("click", function() {

			$.get(configs.edit_url, {
				id : $(this).data("id")
			}, function(res) {
				if ( res.code == '000' ) {

					exports.post(res.data, configs.update_url);

				} else {
					JDialog.tip.work({
						type: __global.jdialog.messageType[res.code],
						content:res.message,
						lock : true,
						timer:__global.jdialog.timer
					});
				}
			}, 'json')

		});

		/**-- deleteopt --**/
		$(".js-delete").click(function(){
			var id = $(this).attr('data-id');
			layer.confirm('确定要删除吗',['确定','取消'],function(){
				layer.load();
				$.post(exports.deleteUrl,{id:id},function(data){
					layer.closeAll('loading');
					if(data.code == 0 || data.code=='success'){
						layer.msg('删除成功');
						document.location.reload();
					}else{
						layer.msg(data.message);
					}
				},'json');
			});
		});
		

		$(".js-edit").click(function(){
			id = $(this).attr('data-id');
			/**--tmpl-editlayer--**/
			$.get(exports.detailUrl,{id:id},function(data){
		
				var options = {
					type : 1,
					area : ['800px','500px'],
					content : JTemplate.render("editpanel",data),
					btn : {
						'确定' :function(){
							var form = $("#editpanel-form");
							form.validate({
								rules: {

										   /**--tmpl-editrules--**/

									   },
								messages : {  
											   /**--tmpl-editmessages--**/
										   },  

								submitHandler : function(form){

									$(form).find('input[name=id]').remove();
									$(form).append("<input type='hidden' name='id' value='"+id+"' >");
									var data = $(form).serialize();
									common.ajaxResult(exports.editUrl,data);

								}
							});
							form.submit();
						},
						"取消" : function(){
						
						}
					}
				};
				var options = $.extend(__global.jdialog, options);
				var popWin = JDialog.win.work(options);

				/**--tmpl-edittodo--**/
				
			},'json');
			
		});
		/**--tmpl-othertodo--**/
	};


	/**
	 * 添加或者编辑数据
	 * @param item 表单初始化的数据
	 * @param url 表单提交的url地址
	 */
	exports.post = function(item, url) {

		var html = template('content-add-template', {item:item});
		var options = {
			title : item.id > 0 ? "编辑分组" : "添加分组",
			content : html,
			icon : 'none',
			width : 600,
			height : 380,
			effect : 0, //切换过度特效， 0， 1， 2
			//container : '#right-section',
			button : {
				'保存' : function() {
					var formId = 'content-add-form';
					if ( !common.formCheckHandler(formId, false) ) return false;
					//collect form data
					var formData = $('#' + formId).serialize();
					popWin.lock();
					$.post(url, formData, function (data) {

						popWin.close();
						JDialog.tip.work({
							type: __global.jdialog.messageType[data.code],
							//container:'#right-section',
							//top : __global.jdialog.top,
							content:data.message,
							timer:__global.jdialog.timer
						});

						if ( data.code == '000' ) { //操作成功刷新页面
							setTimeout(function() {location.reload();}, __global.reloadDelayTime);
						}

					}, 'json');

				},
				'取消' : function() {
					popWin.close();
				}
			}
		};
		options = $.extend(__global.jdialog, options);
		var popWin = JDialog.win.work(options);

		//common.initElements();
		//初始化开关
		$(".bswitch").bootstrapSwitch({
			onColor : 'success',
			offColor : 'danger'
		});
		common.bswitchAddInit();
	}

	//批量删除
	$('select[name=action-bacth-select]').click(function (e) {

		if (!common.hasChecked) return;
		e.stopPropagation();

		var actionName=$(this).children('option:selected').val();

		switch(actionName){

			case 'delete':common.list_bacth_delete();break;
			case 'on':exports.list_bacth_on();break;
			case 'off':exports.list_bacth_off();break;
		}

	});

	//批量操作
	$(".inner-container [data-act='operateAll']").on("click","a",function(){
		var actionName = $(this).data("action");
		switch(actionName){
			case 'delete':common.list_bacth_delete();break;
			case 'on':exports.list_bacth_on();break;
			case 'off':exports.list_bacth_off();break;
		}
	});

	//批量启用
	exports.list_bacth_on=function(){
		var urlpathinfo = document.location.pathname.split('/');
		var url = '/' + urlpathinfo[1] + '/' + urlpathinfo[2] + '/do_updateStatus?type=1';
		var __confirm = JDialog.win.work({
			title: "启用",
			width: 340,
			height: 180,
			borderWidth: 8,
			lock: true,
			effect: 0,
			content: '<div style="padding-top: 15px;">确定要批量启用这些记录吗？</div>',
			icon: 'warn',
			button: {
				'确认': function () {
					$.post(url, $("#J_List_Form").serialize(), function (data) {
						__confirm.close();
						if (data.code == "000") {
							JDialog.tip.work({type: "ok", content: data.message, timer: 1000});
							setTimeout(function () {
								location.reload();
							}, 1000)
						} else {
							JDialog.tip.work({type: "error", content: data.message, timer: __global.jdialog.timer});
						}
					}, "json");
				},
				'取消': function () {
					__confirm.close();
				}
			}

		});


	};
	//批量禁用
	exports.list_bacth_off=function(){

		var urlpathinfo = document.location.pathname.split('/');
		var url = '/' + urlpathinfo[1] + '/' + urlpathinfo[2] + '/do_updateStatus?type=0';
		var __confirm = JDialog.win.work({
			title: "禁用",
			width: 340,
			height: 180,
			borderWidth: 8,
			lock: true,
			effect: 0,
			content: '<div style="padding-top: 15px;">确定要批量禁用这些记录吗？</div>',
			icon: 'warn',
			button: {
				'确认': function () {
					$.post(url, $("#J_List_Form").serialize(), function (data) {
						__confirm.close();
						if (data.code == "000") {
							JDialog.tip.work({type: "ok", content: data.message, timer: 1000});
							setTimeout(function () {
								location.reload();
							}, 1000)
						} else {
							JDialog.tip.work({type: "error", content: data.message, timer: __global.jdialog.timer});
						}
					}, "json");
				},
				'取消': function () {
					__confirm.close();
				}
			}

		});

	};

});
